package com.demo111_二叉树的最小深度;

public class Solution {
    public int minDepth(TreeNode root) {
        int max = 0;
        if (root==null){
            return max;
        }
        max++;
        int left = dfs(root.left, 1);
        int right = dfs(root.right, 1);

        if (right==1||left==1){
            return Math.max(right,left);
        }
        return Math.min(left,right);
    }

    private int dfs(TreeNode root, int max) {
        if (root==null){
            return max;
        }
        max++;
        int temp=max;
        int resultLeft = dfs(root.left, temp);
        max=Math.max(resultLeft,max);
        int resultRight = dfs(root.right, temp);
        max=Math.max(resultRight,max);
        return max;

    }
}
class TreeNode {
    int val ;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}